# -*- coding: utf8 -*-

from django.db import models
from django.contrib.auth.models import User

##########
# Models #
##########

class svn_repo(models.Model):
    name = models.CharField(max_length=80)
    description = models.TextField()
    server_host = models.CharField(max_length=80)
    server_user = models.CharField(max_length=25)
    server_password = models.CharField(max_length=80)
    repo_path = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name+u' ('+self.server_host+u')'
    
    
class svn_user(models.Model):
    name = models.CharField(max_length=80)
    password = models.CharField(max_length=80)

    def __unicode__(self):
        return self.name
    
class svn_group(models.Model):
    name = models.CharField(max_length=80)
    users = models.ManyToManyField(svn_user)

    def __unicode__(self):
        return u'@'+self.name

class svn_project(models.Model):
    name = models.CharField(max_length=80)
    description = models.TextField()
    repo = models.ForeignKey(svn_repo)

    def __unicode__(self):
        return self.name+u' ('+unicode(self.repo)+u')'

class svn_path(models.Model):
    name = models.CharField(max_length=80)
    project = models.ForeignKey(svn_project)
    parent = models.ForeignKey('self', blank=True, null=True)
    
    def get_path(self):
        if self.parent is None:
            return self.name
        return self.parent.get_path()+self.name
    
    

    def __unicode__(self):
        return self.get_path()+u' ('+unicode(self.project)+u')'

class svn_right(models.Model):
    RIGHT_CHOICES = (
                     (u'', u'Brak uprawnień'),
                     (u'r', u'Możliwość odczytu'),
                     (u'w', u'Możliwość zapisu (bez odczytu)'),
                     (u'rw', u'Możliwość odczytu i zapisu'),
                     )
    right = models.CharField(max_length=2, choices = RIGHT_CHOICES)
    order = models.IntegerField()
    path = models.ForeignKey(svn_path)

    def __unicode__(self):
        return u'Right'+u' ('+unicode(self.path)+u')'

class svn_user_right(svn_right):
    for_who = models.ForeignKey(svn_user)

    def __unicode__(self):
        return u'Right '+unicode(self.for_who)+u' ('+unicode(self.path)+u')'

class svn_group_right(svn_right):
    for_who = models.ForeignKey(svn_group)

    def __unicode__(self):
        return u'Right '+unicode(self.for_who)+u' ('+unicode(self.path)+u')'

class svn_forall_right(svn_right):
    for_who = u'*'

    def __unicode__(self):
        return u'Right '+unicode(self.for_who)+u' ('+unicode(self.path)+u')'
